$(document).ready(function(){
	$('#user_form').submit(function(){
		return false;
	});
	$('#btn_search_user').click(function(){
		searchUser();
	});
	$('#btn_add_user').click(function(){
		addUser();
	});
	$('#btn_update_user').click(function(){
		updateUser();
	});
	$('#btn_delete_user').click(function(){
		deleteUser();
	});
	$('#btn_clear_user').click(function(){
		$('#username').val('');
		$('#current_username').val('');
		$('#password').val('');
		$('#repassword').val('');
		$('#email').val('');
		$("#optionsRadios1").prop("checked", true);
		$('#submit_error').html('');
	});
	
});

function searchUser(){
	var username = $('#username').val();
	$('#submit_error').html('');
	if(username !== ''){
		var data = {
			username: username
		};
		var request = {
			url: base_url + GET_USER,
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					var userData = JSON.parse(data);
					$('#email').val(userData.email);
					$('#current_username').val(userData.username);
					if(userData.level == 1){
						$("#optionsRadios1").prop("checked", true);
					} else {
						$("#optionsRadios2").prop("checked", true);
					}
				} else {
					$('#submit_error').html(ERROR_ID_NOT_FOUND);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function addUser(){
	var username = $('#username').val();
	var password = $('#password').val();
	var repassword = $('#repassword').val();
	var email = $('#email').val();
	var level = $("input[type='radio'][name='level']:checked").val();
	$('#submit_error').html('');
	if(password == repassword && password !== ''){
		if(username !== '' && password !== '' && email !== ''){
			var data = {
				username: username,
				password: password,
				email: email,
				level: level
			};
			var request = {
				url: base_url + ADD_USER,
				type: 'POST',
				data: data,
				success: function(data){
					if(data == '1'){
						$('#submit_error').html(RESPONSE_SUCCESS);
					} else {
						$('#submit_error').html(RESPONSE_ERROR);
					}
				},
				error: function(error){
					$('#submit_error').html(RESPONSE_ERROR);
				}
			};
			excuteAjax(request);
		} else {
			$('#submit_error').html(ERROR_INPUT);
		}
	} else {
		$('#submit_error').html('Mật khẩu không trùng khớp');
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function updateUser(){
	var username = $('#current_username').val();
	var password = $('#password').val();
	var repassword = $('#repassword').val();
	var email = $('#email').val();
	var level = $("input[type='radio'][name='level']:checked").val();
	$('#submit_error').html('');
	if(password == repassword){
		if(username !== '' && email !== ''){
			var data = {
				username: username,
				password: password,
				email: email,
				level: level
			};
			var request = {
				url: base_url + UPDATE_USER,
				type: 'POST',
				data: data,
				success: function(data){
					$('#submit_error').html(RESPONSE_SUCCESS);
				},
				error: function(error){
					$('#submit_error').html(RESPONSE_ERROR);
				}
			};
			excuteAjax(request);
		} else {
			$('#submit_error').html(ERROR_INPUT);
		}
	} else {
		$('#submit_error').html('Mật khẩu không trùng khớp');
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function deleteUser(){
	var username = $('#current_username').val();
	$('#submit_error').html('');
	if(username !== ''){
		var data = {
			username: username
		};
		var request = {
			url: base_url + DELETE_USER,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}